package com.ruoyi.web.controller.ai;

import com.ruoyi.common.ai.BaseResponse;
import com.ruoyi.common.ai.ErrorCode;
import com.ruoyi.common.ai.ResultUtils;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.exception.ThrowUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.DocumentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;


/**
 * 文档控制器
 *
 * @author FangDaniu
 * @since 2024/06/08
 */
@Tag(name = "文档接口")
@RestController
@RequestMapping("/document")
public class DocumentController {

    @Resource
    private DocumentService documentService;

    @Operation(summary = "查询所有文档")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse<List<DocumentsResponse.FolderItem>> listDocuments() {
        DocumentsResponse.LocalFiles documents = documentService.getAllDocuments();

        return ResultUtils.success(documents.getItems());
    }

    @Operation(summary = "上传文档")
    @PostMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse<List<DocumentResponse>> uploadDocument(@RequestParam("file") MultipartFile multipartFile) throws IOException {

        File file = convertMultiPartToFile(multipartFile);
        DocumentResponseWrapper responseBody = documentService.uploadDocument(file);

        boolean deleted = file.delete();
        ThrowUtils.throwIf(!deleted, new BusinessException(ErrorCode.OPERATION_ERROR, "File not deleted"));

        return ResultUtils.success(responseBody.getDocuments());
    }

    @Operation(summary = "查询工作区文档")
    @GetMapping(value = "/workspace/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse<List<WorkspaceGetResponse.Document>> listWorkspace() {

        List<WorkspaceGetResponse.Document> documents = documentService.getWorkDocuments();

        return ResultUtils.success(documents);
    }

    @Operation(summary = "移出工作区文档")
    @DeleteMapping(value = "remove/workspace/{workspaceName}", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse<WorkspaceUpdateResponse> removeListWorkspace(
            @PathVariable(value = "workspaceName" )String workspaceName
            ,@RequestParam("docpath") String docpath
    ) {

        WorkspaceUpdateResponse deleteResponse = documentService.removeAllDocuments(workspaceName,docpath);

        return ResultUtils.success(deleteResponse);
    }

    @Operation(summary = "移入工作区文档")
    @GetMapping(value = "add/workspace/{workspaceName}", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse<WorkspaceUpdateResponse> addListWorkspace(
            @PathVariable(value = "workspaceName" )String workspaceName
            ,@RequestParam("name") String docpath
    ) {

        WorkspaceUpdateResponse deleteResponse = documentService.addAllDocuments(workspaceName,docpath);

        return ResultUtils.success(deleteResponse);
    }

    private File convertMultiPartToFile(MultipartFile file) throws IOException {
        File convFile = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename());
        file.transferTo(convFile);
        return convFile;
    }

}

